package com.visikard.ui.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.visikard.item.ItemRedeemDetails;
import com.visikard.ui.R;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;
import com.visikard.utility.Utils;

public class TransactionDetailSpecialsKardAdapter extends BaseAdapter {

	Context context;
	ArrayList<ItemRedeemDetails> data;
	LayoutInflater inflater;

	String userName;
	DisplayMetrics metrics;
	int isCoupon = 0;
	String mUrl;

	public TransactionDetailSpecialsKardAdapter(Context ct,
			ArrayList<ItemRedeemDetails> dt, String userName, int isCoupon, String mUrl) {
		this.context = ct;
		this.data = dt;
		this.userName = userName;
		this.isCoupon = isCoupon;
		this.mUrl = mUrl;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		metrics = context.getResources().getDisplayMetrics();
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		if (position < data.size()) {
			return data.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_transaction_detail_gift_kard,
					null);
			Utils.overrideFonts(convertView);
			viewHolder = new ViewHolder();
			viewHolder.mUsername = (TextView) convertView.findViewById(R.id.text_transaction_username);
			viewHolder.icon = (ImageView) convertView.findViewById(R.id.redeem_transaction_detail_icon);
			viewHolder.mTime = (TextView) convertView.findViewById(R.id.text_transaction_time);
			viewHolder.mRedemptionValue = (TextView) convertView.findViewById(R.id.text_transaction_redemptionValue);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		ItemRedeemDetails item = (ItemRedeemDetails) getItem(position);
		if (item != null) {
			int mWidth = metrics.widthPixels / 4;

			RelativeLayout.LayoutParams lyKard = (RelativeLayout.LayoutParams) viewHolder.icon
					.getLayoutParams();
			int kardHeight = mWidth * 2 / 3;
			lyKard.width = kardHeight * 2 / 3;
			lyKard.height = kardHeight;
			
//			ImageLoader.getInstance(context).loadImageCorner(viewHolder.icon, mUrl,
//					Device.getDeviceWidth() / 5, BitmapUtils.ROUND_ALL, 4, 3, R.drawable.kard_shadow_default_horizontal, 0,  0);
			UILImageUtils.loadImage(viewHolder.icon, //image view
					mUrl, //url
					1, //size of view
					BitmapUtils.ROUND_ALL, //round all 
					4, // round size
					3, //shadow
					R.drawable.kard_shadow_default_horizontal, //shadow resource 
					0, 0); 
			
			viewHolder.mUsername.setText(userName);
			try {
				if(isCoupon == 1)
					viewHolder.mRedemptionValue.setText(context.getResources().getString(R.string.transactions_redeem_detail_redeemed) + 
							": " + item.getRedemptionValue());
				else
					viewHolder.mRedemptionValue.setText(context.getResources().getString(R.string.transactions_redeem_detail_redeemed)
							+ ": $" + String.format("%.2f", Double.parseDouble(item.getRedemptionValue())));
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			viewHolder.mTime.setText(Utils.formatMessageDateTime(context, item.getDateTimeWithTimezone()));
		}
		return convertView;
	}

	private class ViewHolder {
		TextView mUsername, mTime, mRedemptionValue;
		ImageView icon;
	}

}
